<?php require_once('../Connections/learnConn.php'); ?>
<?php
if (!isset($_SESSION)) {
  session_start();
}
$MM_authorizedUsers = "guru";
$MM_donotCheckaccess = "false";

// *** Restrict Access To Page: Grant or deny access to this page
function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) { 
  // For security, start by assuming the visitor is NOT authorized. 
  $isValid = False; 

  // When a visitor has logged into this site, the Session variable MM_Username set equal to their username. 
  // Therefore, we know that a user is NOT logged in if that Session variable is blank. 
  if (!empty($UserName)) { 
    // Besides being logged in, you may restrict access to only certain users based on an ID established when they login. 
    // Parse the strings into arrays. 
    $arrUsers = Explode(",", $strUsers); 
    $arrGroups = Explode(",", $strGroups); 
    if (in_array($UserName, $arrUsers)) { 
      $isValid = true; 
    } 
    // Or, you may restrict access to only certain users based on their username. 
    if (in_array($UserGroup, $arrGroups)) { 
      $isValid = true; 
    } 
    if (($strUsers == "") && false) { 
      $isValid = true; 
    } 
  } 
  return $isValid; 
}

$MM_restrictGoTo = "../index.php";
if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) {   
  $MM_qsChar = "?";
  $MM_referrer = $_SERVER['PHP_SELF'];
  if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&";
  if (isset($QUERY_STRING) && strlen($QUERY_STRING) > 0) 
  $MM_referrer .= "?" . $QUERY_STRING;
  $MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer);
  header("Location: ". $MM_restrictGoTo); 
  exit;
}
?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO nilai (nis, kd_kelas, kd_kat_kelas, kd_matpel, tahun_ajaran, semester, ul1, ul2, ul3, ul4, ul5, tgs1, tgs2, tgs3, tgs4, tgs5, uts, uas, nilai_akhir) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['nis'], "int"),
                       GetSQLValueString($_POST['kd_kelas'], "text"),
                       GetSQLValueString($_POST['kd_kat_kelas'], "text"),
                       GetSQLValueString($_POST['kd_matpel'], "text"),
                       GetSQLValueString($_POST['tahun_ajaran'], "text"),
                       GetSQLValueString($_POST['semester'], "text"),
                       GetSQLValueString($_POST['ul1'], "double"),
                       GetSQLValueString($_POST['ul2'], "double"),
                       GetSQLValueString($_POST['ul3'], "double"),
                       GetSQLValueString($_POST['ul4'], "double"),
                       GetSQLValueString($_POST['ul5'], "double"),
                       GetSQLValueString($_POST['tgs1'], "double"),
                       GetSQLValueString($_POST['tgs2'], "double"),
                       GetSQLValueString($_POST['tgs3'], "double"),
                       GetSQLValueString($_POST['tgs4'], "double"),
                       GetSQLValueString($_POST['tgs5'], "double"),
                       GetSQLValueString($_POST['uts'], "double"),
                       GetSQLValueString($_POST['uas'], "double"),
                       GetSQLValueString($_POST['nilai_akhir'], "double"));

  mysql_select_db($database_learnConn, $learnConn);
  $Result1 = mysql_query($insertSQL, $learnConn) or die(mysql_error());

  $kp = $_GET['kp'];
  $k = $_GET['k'];
  $ks = $_GET['ks'];
  $insertGoTo = "list_siswa.php?kode_matpel=$kp&kode_kelas=$k&kode_sub_kelas=$ks";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}

mysql_select_db($database_learnConn, $learnConn);
$query_thn = "SELECT * FROM tahun_ajaran ORDER BY tahun_ajaran DESC";
$thn = mysql_query($query_thn, $learnConn) or die(mysql_error());
$row_thn = mysql_fetch_assoc($thn);
$totalRows_thn = mysql_num_rows($thn);

$colname_kelas = $_GET['k'];
  $colname_kelas1 = $_GET['ks'];
mysql_select_db($database_learnConn, $learnConn);
$query_kelas = sprintf("SELECT * FROM kelas WHERE kelas = %s AND sub_kelas=%s", 
					   GetSQLValueString($colname_kelas, "int"),
					   GetSQLValueString($colname_kelas1, "text"));

$kelas = mysql_query($query_kelas, $learnConn) or die(mysql_error());
$row_kelas = mysql_fetch_assoc($kelas);
$totalRows_kelas = mysql_num_rows($kelas);
$colname_nis = $row_kelas['kd_kelas'];

mysql_select_db($database_learnConn, $learnConn);
$query_nis = sprintf("SELECT * FROM siswa_kelas WHERE kd_kelas = %s", GetSQLValueString($colname_nis, "text"));
$nis = mysql_query($query_nis, $learnConn) or die(mysql_error());
$row_nis = mysql_fetch_assoc($nis);
$totalRows_nis = mysql_num_rows($nis);


$colname_matapel = "-1";
if (isset($_GET['kp'])) {
  $colname_matapel = $_GET['kp'];
} 
mysql_select_db($database_learnConn, $learnConn);
$query_matapel = sprintf("SELECT * FROM matpel WHERE kd_matpel = %s", GetSQLValueString($colname_matapel, "text"));
$matapel = mysql_query($query_matapel, $learnConn) or die(mysql_error());
$row_matapel = mysql_fetch_assoc($matapel);
$totalRows_matapel = mysql_num_rows($matapel); 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Input Nilai</title>
<link href="../style/style.css" type="text/css" rel="stylesheet" />
<script language="javascript" type="text/javascript">
 function hitung() {
	 var ul1 = parseInt(document.form1.ul1.value);
	 var ul2 = parseInt(document.form1.ul2.value);
	 var ul3 = parseInt(document.form1.ul3.value);
	 var ul4 = parseInt(document.form1.ul4.value);
	 var ul5 = parseInt(document.form1.ul5.value);
	 
	 var tgs1 = parseInt(document.form1.tgs1.value);
	 var tgs2 = parseInt(document.form1.tgs2.value);
	 var tgs3 = parseInt(document.form1.tgs3.value);
	 var tgs4 = parseInt(document.form1.tgs4.value);
	 var tgs5 = parseInt(document.form1.tgs5.value);
	 
	 var uts = parseInt(document.form1.uts.value);
	 var uas = parseInt(document.form1.uas.value);
	 
	 if (ul1 != 0 && ul2 !=0 && ul3 != 0 && ul4 !=0 && ul5 !=0) {
		 var i = 5;
	 }
	 else if (ul1 != 0 && ul2 !=0 && ul3 != 0 && ul4 !=0 && ul5 ==0) {
		 var i = 4;
	 }
	 
	 else if (ul1 != 0 && ul2 !=0 && ul3 != 0 && ul4 ==0 && ul5 ==0) {
		 var i = 3;
	 }
	 
	 else if (ul1 != 0 && ul2 !=0 && ul3 == 0 && ul4 ==0 && ul5 ==0) {
		 var i = 2;
	 }
	 
	 else if (ul1 != 0 && ul2 ==0 && ul3 == 0 && ul4 ==0 && ul5 ==0) {
		 var i = 1;
	 }
	 
	 var totUl = (ul1 + ul2 + ul3 + ul4 + ul5) / i;
	 var totUl2 = totUl * 0.6;
	 var totUts = uts * 0.2;
	 var totUas = uas * 0.2;
	 var totAll = totUl2 + totUts + totUas ;
	 
	 var aaa = totAll.toFixed(2);
	 document.form1.nilai_akhir.value = aaa;
 }
</script>
<link href="../style/pro_drop_1.css" type="text/css" rel="stylesheet"/>
<script src="../style/stuHover.js" type="text/javascript"></script>
</head>

<body><center>
<table width="900" bgcolor="#FFFFFF">
	<?php
	include "topmenu_admin.php";
	?>
    <tr>
    	<td id="menu">
        <ul>
        	<li><a href="index.php">Home</a></li>
        	<li><a href="nilai.php">Daftar Kelas</a></li>
   	    <li><a href="jadwal.php">Jadwal</a></li>
        	<li><a href="passE.php">Ubah Password</a></li>
            <li><a href="../logout.php">Logout</a></li>
        </ul>
        </td>
    	<td id="konten">
         
      <p>&nbsp;</p>
      <form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
        <table align="center">
          <tr valign="baseline">
            <td nowrap="nowrap" align="right">Kelas :</td>
            <td><?php echo $_GET['k']." - ".$_GET['ks']; ?></td>
          </tr>
          <tr valign="baseline">
            <td nowrap="nowrap" align="right">Mata Pelajaran :</td>
            <td><?php echo $row_matapel['nama_matpel'] ?></td>
          </tr>
          <tr valign="baseline">
            <td nowrap="nowrap" align="right">Tahun Ajaran :</td>
            <td><select name="tahun_ajaran">
                <?php do { ?>  <option value="<?php echo $row_thn['tahun_ajaran']; ?>"><?php echo $row_thn['tahun_ajaran']; ?></option>
                <?php } while ($row_thn = mysql_fetch_assoc($thn)); ?>
                </select></td>
          </tr>
          <tr valign="baseline">
            <td nowrap="nowrap" align="right">NIS:</td>
            <td>
                <select name="nis">
                <?php do { ?>  <option value="<?php echo $row_nis['nis']; ?>"><?php echo $row_nis['nis']; ?></option>
                <?php } while ($row_nis = mysql_fetch_assoc($nis)); ?>
                </select>
                </td>
          </tr>
          <tr valign="baseline">
            <td nowrap="nowrap" align="right">Semester:</td>
            <td><select name="semester">
            	<option value="ganjil">Ganjil</option>
            	<option value="genap">Genap</option>
            </select></td>
          </tr>
          <tr valign="baseline">
            <td nowrap="nowrap" align="right">Ulangan 1:</td>
            <td><input type="text" name="ul1" value="0" onchange="hitung()" size="32" /></td>
          </tr>
          <tr valign="baseline">
            <td nowrap="nowrap" align="right">Ulangan 2:</td>
            <td><input type="text" name="ul2" value="0" onchange="hitung()" size="32" /></td>
          </tr>
          <tr valign="baseline">
            <td nowrap="nowrap" align="right">Ulangan 3:</td>
            <td><input type="text" name="ul3" value="0" onchange="hitung()" size="32" /></td>
          </tr>
          <tr valign="baseline">
            <td nowrap="nowrap" align="right">Ulangan 4:</td>
            <td><input type="text" name="ul4" value="0" onchange="hitung()" size="32" /></td>
          </tr>
          <tr valign="baseline">
            <td nowrap="nowrap" align="right">Ulangan 5:</td>
            <td><input type="text" name="ul5" value="0" size="32" onchange="hitung()" /></td>
          </tr>
          <tr valign="baseline">
            <td nowrap="nowrap" align="right">Tugas 1:</td>
            <td><input type="text" name="tgs1" value="0" size="32" onchange="hitung()" /></td>
          </tr>
          <tr valign="baseline">
            <td nowrap="nowrap" align="right">Tugas 2:</td>
            <td><input onchange="hitung()" type="text" name="tgs2" value="0" size="32" /></td>
          </tr>
          <tr valign="baseline">
            <td nowrap="nowrap" align="right">Tugas 3:</td>
            <td><input type="text" name="tgs3" onchange="hitung()" value="0" size="32" /></td>
          </tr>
          <tr valign="baseline">
            <td nowrap="nowrap" align="right">Tugas 4:</td>
            <td><input type="text" name="tgs4" value="0" size="32" onchange="hitung()" /></td>
          </tr>
          <tr valign="baseline">
            <td nowrap="nowrap" align="right">Tugas 5:</td>
            <td><input type="text" name="tgs5" value="0" size="32" onchange="hitung()" /></td>
          </tr>
          <tr valign="baseline">
            <td nowrap="nowrap" align="right">UTS:</td>
            <td><input type="text" name="uts" value="0" size="32" onchange="hitung()" /></td>
          </tr>
          <tr valign="baseline">
            <td nowrap="nowrap" align="right">UAS:</td>
            <td><input type="text" name="uas" value="0" size="32" onchange="hitung()" /></td>
          </tr>
          <tr valign="baseline">
            <td nowrap="nowrap" align="right">Nilai_akhir:</td>
            <td><input name="nilai_akhir" type="text" value="0" size="32" readonly="readonly" /></td>
          </tr>
          <tr valign="baseline">
            <td nowrap="nowrap" align="right">&nbsp;</td>
            <td><input type="submit" value="Tambah Nilai" /></td>
          </tr>
        </table>
        <input type="hidden" name="kd_kelas" value="<?php echo $_GET['k']; ?>" />
        <input type="hidden" name="kd_kat_kelas" value="<?php echo $_GET['ks']; ?>" />
        <input type="hidden" name="kd_matpel" value="<?php echo $_GET['kp']; ?>" />
        <input type="hidden" name="MM_insert" value="form1" />
      </form>
      <p>&nbsp;</p></td>
    </tr>
    <tr>
    	<td colspan="2" id="footer">Copyright 2012</td>
    </tr>
    
</table></center>
<p>&nbsp;</p>
</body>
</html>
<?php
mysql_free_result($thn);

mysql_free_result($matapel);

mysql_free_result($nis);

mysql_free_result($kelas);

?>
